﻿using DTCash.Logger;
using DTCash.Logger.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTCash.Redis;
using Uranus.Service.CompanyService;

namespace Uranus.Service
{
    public class Company : BaseData
    {
        private static Company company = null;

        public Company()
        {

        }

        public static Company GetCompany()
        {
            if (company == null)
            {
                company = new Company();
            }
            return company;
        }

        /// <summary>
        /// 获取担保机构信息根据ID
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public CompanyResponse GetCompany(int companyId)
        {
            CompanyServiceClient client = new CompanyServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(companyId.ToString(), "GetCompany").Product.Info;
                Func<CompanyResponse> method = delegate
                {
                    return client.GetCompany(companyId);
                };
                return base.GetData<CompanyResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Company.GetCompany 出现异常", ex.Message.ToString());

                return null;
            }
            finally
            {
                client.Close();
            }
        }

        /// <summary>
        /// 示例 查询产品担保机构统计信息
        /// Date:2017-12-07 20:38:29
        /// Author:Linsan
        /// </summary>
        /// <param name="request">机构编号</param>
        /// <returns></returns>
        public CompanyStatisticsBaseResponse CompanyStatistics(CompanyStatisticsBaseRequest request)
        {
            CompanyServiceClient client = new CompanyServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(request.CompanyId.ToString(), "CompanyStatistics").Product.Info;
                Func<CompanyStatisticsBaseResponse> method = delegate
                {
                    return client.CompanyStatistics(request);
                };
                return base.GetData<CompanyStatisticsBaseResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Company.CompanyStatistics 出现异常", ex.Message.ToString());

                return null;
            }
            finally
            {
                client.Close();
            }
        }

    }
}
